define(['moment'], function (moment) {
    return ['$rootScope', '$scope', '$filter', '$stateParams', 'config', 'MMWService', '$timeout',"$calculater",
        function ($rootScope, $scope, $filter, $stateParams, config, $MMWService, $timeout, $calculater) {
            $scope.init = function(){
                $scope.type = '0';          //預設進箱
                $scope.reel_no = '';        //物料編碼
                $scope.material_no = '';    //物料料號
                $scope.material_list = [];  //物料清單

                $scope.find_material();
            };

            $scope.back = function(){
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('home', true);
            };

            // QRCode輸入
            $scope.QRCodeScanner = function(){
                $scope.$root.OpenScanner({
                    after : function(result){
                        $scope.barcode_no = result.text;
                        input_barcode_no_callback();
                    },
                    exception: function(){
                        //目前裝置無法使用相機
                        $scope.$root.showAlert($filter('translate')('B0214.showAlert.column3'));
                    },
                    orientation : 'landscape'
                });
            };

            // barcode輸入
            $scope.input_barcode_no = function(event){
                if(event.keyCode == 13){
                    $scope.boxAction();
                }
            };

            // 查詢序號
            $scope.boxAction = function (){
                let inbox = $scope.material_list.some(function(item){
                    return item.reel_no == $scope.barcode_no;
                })
                if($scope.barcode_no!=''){
                    if($scope.type == 0){//進箱
                        if(inbox == true){
                            $rootScope.showAlert($filter('translate')('SMT2201.msg.already_inbox'));
                            $scope.barcode_no='';
                        }else{
                            reel_process();
                        }
                    }else if($scope.type == 1){//出箱
                        if(inbox == true){
                            reel_process();
                        }else{
                            $rootScope.showAlert($filter('translate')('SMT2201.msg.already_out_of_box'));
                            $scope.barcode_no='';
                        }
                    }
                }
            }

            function reel_process(){
                var barcode = $scope.barcode_no;
                $scope.barcode_no = '';
                if(barcode != ''){
                    $MMWService.sendToServer({
                        uri:'ServicesSMT.Module_SMT.dry_reel_process',
                        content : {
                            reel_no:barcode,
                            type:$scope.type
                        },
                        success: function(data){
                            // $scope.material_list = data.dry_list;
                            $scope.find_material(barcode);
                        }
                    });
                }
            }

            //查詢
            $scope.find_material = function(reel_no){
                let content = {
                    reel_no:$scope.reel_no,
                    material_no:$scope.material_no
                }
                if(reel_no)content.reel_no = reel_no;
                $MMWService.sendToServer({
                    uri:'ServicesSMT.Module_SMT.dry_reel_info_get',
                    content : content,
                    success: function(data){
                        $scope.material_list = data; 
                        $scope.reel_no = '';
                        $scope.material_no = '';
                        // if(data.length == 0){
                            // $rootScope.showAlert($filter('translate')('common.msg.no_data'));
                            
                        // }
                    }
                });
            }

            //清除
            $scope.clean = function(){
                $scope.reel_no = '';
                $scope.material_no = '';
                $scope.material_list = [];
            }
        }
    ];
});
